package com.zzl.ov_library.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzl.ov_library.dto.Result;
import com.zzl.ov_library.entity.Branch;
import com.zzl.ov_library.entity.BranchVO;
import com.zzl.ov_library.entity.Reservation;
import com.zzl.ov_library.service.BranchService;
import com.zzl.ov_library.service.ReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class BranchController {
    @Autowired
    private BranchService branchService;

    @Autowired
    private ReservationService reservationService;

    //添加分馆
    @PostMapping("/branch/add")
    public Result addBranch(String branchName, String branchAddress, Integer max) {
        //通过分馆名查询分馆是否存在
        Branch branch = branchService.getOne(Wrappers.lambdaQuery(Branch.class)
                .eq(Branch::getBranchName, branchName));
        if (branch != null) {
            return Result.fail().setMsg("分馆已存在");
        }
        branch = new Branch();
        branch.setBranchName(branchName);
        branch.setAddress(branchAddress);
        branch.setMax(max);
        return branchService.save(branch) ? Result.ok().setMsg("添加成功") : Result.fail().setMsg("添加失败");
    }

    //通过分馆id查询分馆信息
    @GetMapping("/branch/query/{branchId}")
    public Result queryBranch(@PathVariable Integer branchId) {
        Branch branch = branchService.getById(branchId);
        return branch != null ? Result.ok().setData(branch) : Result.fail().setMsg("分馆不存在");
    }

    //查询分馆列表
    @GetMapping("/branch/list")
    public Result list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer limit,
                       @RequestParam String day) {
        List<Branch> list = branchService.list(new Page<>(page, limit));
        long count = branchService.count();

        //获取当前年月
        LocalDate now = LocalDate.now();
        String reserveTime = now.getYear() + "-" + now.getMonthValue() + "-" + day;

        List<BranchVO> branchVOList = new ArrayList<>();
        //遍历集合，查询reservation表获取该分馆当天预约人数
        for (Branch branch : list) {
            Long resCount = reservationService.count(Wrappers.lambdaQuery(Reservation.class)
                    .eq(Reservation::getBranchId, branch.getBranchId())
                    .eq(Reservation::getReserveTime, reserveTime)
                    .in(Reservation::getStatus, "待签到", "已签到"));
            //将分馆信息封装到BranchVO中
            BranchVO branchVO = new BranchVO();
            branchVO.setBranchId(branch.getBranchId());
            branchVO.setBranchName(branch.getBranchName());
            branchVO.setAddress(branch.getAddress());
            branchVO.setMax(branch.getMax());
            branchVO.setStartTime(branch.getStartTime());
            branchVO.setEndTime(branch.getEndTime());
            branchVO.setResCount(resCount.intValue());
            branchVOList.add(branchVO);
        }
        System.out.println("branchVOList:" + branchVOList);
        return Result.ok().setData(branchVOList).setCount(count).setMsg("查询成功");
    }

    //查询分馆列表
    @GetMapping("/branch/list2")
    public Result list2(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer limit) {
        List<Branch> list = branchService.list(new Page<>(page, limit));
        long count = branchService.count();
        return Result.ok().setData(list).setCount(count).setMsg("查询成功");
    }

}
